//
//  BRSLIsenceControllerViewController.m
//  Pint
//
//  Created by xiect on 13-10-22.
//  Copyright (c) 2013年 brains. All rights reserved.
//
#import "const.h"
#import "BRSEluaControllerViewController.h"

// ELUA表示画面
@interface BRSEluaControllerViewController ()
@property(nonatomic) UIWebView *webView;
@property(nonatomic) UILabel * titleView;
@property(nonatomic) WebViewJavascriptBridge * bridge;

-(void) openUrlStringWithSafari:(NSString*)urlString;
-(void) doAgree;
@end

@implementation BRSEluaControllerViewController
@synthesize webView;
@synthesize titleView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void) loadView {
    [super loadView];
    int webHeight = kSCREEN_HEIGHT;
    NSLog(@"kSCREEN_HEIGHT:%d",webHeight);
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, webHeight)]; // 416 568
    self.titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    self.titleView.text = kSTRING_ELUA;
    self.titleView.textAlignment = NSTextAlignmentCenter;
    // TODO 文字色　文字サイズ
//    self.titleView.textColor = [UIColor colorWithRed:<#(CGFloat)#> green:<#(CGFloat)#> blue:<#(CGFloat)#> alpha:<#(CGFloat)#>];
    
    [self.view addSubview:self.webView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.titleView = self.titleView;
   
    // Bridge 初期化
    self.bridge = [WebViewJavascriptBridge bridgeForWebView:self.webView handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"====>Received message from javascript: %@", data);
        responseCallback(@"Right back atcha");
    }];
    // Agree
    [self.bridge registerHandler:@"btnAgreeClicked" handler:^(id data, WVJBResponseCallback responseCallback) {
        [self doAgree];
    }];
    
    // Disagree
    [self.bridge registerHandler:@"btnDisagreeClicked" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"OC====>==============>btnDisagree");
        // exit app
        [[Utils getAppDelegate] exitApplication];
    }];
    
    // open link with safari
    [self.bridge registerHandler:@"openWithSafari" handler:^(id data, WVJBResponseCallback responseCallback) {
        [self openUrlStringWithSafari:data];
    }];
    
    // read file content
    NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    NSString* path = [[NSBundle mainBundle] pathForResource:@"readme" ofType:@"html"];
    NSString *html = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    [self.webView loadHTMLString:html baseURL:baseURL];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// webview url open with safari
-(void) openUrlStringWithSafari:(NSString*)urlString {
    NSURL *requestURL = [NSURL URLWithString:urlString];
    [[UIApplication sharedApplication] openURL: requestURL];
}

// 同意の場合
-(void) doAgree {
    UIViewController* controller = [[BRSUserTypeViewController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
}
@end
